package com.work.keep.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.work.keep.R;
import com.work.keep.adapter.MySingleWorkoutAdapter;
import com.work.keep.adapter.SingleWorkoutHorAdapter;
import com.work.keep.base.BaseActivity;
import com.work.keep.datas.SingleWorkoutData;
import com.work.keep.datas.SingleWorkoutTimeLineData;
import com.work.keep.volley.GsonRequest;
import com.work.keep.volley.VolleySingleton;

import java.util.HashMap;
import java.util.Map;

/**
 * https://api.gotokeep.com/v1.1/workouts/567cc1f5d1d20e1f26f2a22c
 * https://api.gotokeep.com/v1.1/workouts/567d18d3d1d20e1f26f2a2ac/pioneer
 * https://api.gotokeep.com/v1.1/workouts/567d18d3d1d20e1f26f2a2ac/timeline
 * Created by dllo on 16/1/20.
 * GET https://api.gotokeep.com/v1.1/workouts/567cc1f5d1d20e1f26f2a22c HTTP/1.1
 * X-X: 82241e05792e6420a945b049783997f95de556f4
 * X-KEEP-FROM: android
 * X-KEEP-TIMEZONE: America/New_York
 * X-KEEP-CHANNEL: keep
 * If-None-Match: W/"fde9-Vp4+I7EBp3Q0IS7UowDf1g"
 * X-KEEP-VERSION: 2.9.1
 * Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NjY1MDBlMTYzZGIwYzlmNTdiMTQwYWQiLCJ1c2VybmFtZSI6Iuaeq-WQmyIsImF2YXRhciI6Imh0dHA6Ly93eC5xbG9nby5jbi9tbW9wZW4vc2t3MmhSR3lXRWcwUzV0NzhkMlRVUnFhYjBzcWJ4RXB4S1BLUFpEOXpwbTNJbkIySzRjRnhLVEJsemdDb3VIcGlia3RkRUluQ0NFcGpIS2FHZ1lwRkp6SVRIVU5NYTNpYmYvMCIsImlhdCI6MTQ1MzQ0NDgyNywiZXhwIjoxNDU2MDM2ODI3LCJpc3MiOiJodHRwOi8vd3d3LmdvdG9rZWVwLmNvbS8ifQ.qcYVm3A8SPdiMkjVpp_ZwjLKw0Skws4m2yozvOctsQM
 * User-Agent: Dalvik/2.1.0 (Linux; U; Android 5.1; Google Nexus 5 - 5.1.0 - API 22 - 1080x1920 Build/LMY47D) Paros/3.2.13
 * Host: api.gotokeep.com
 * Connection: Keep-Alive
 * http://static1.gotokeep.com/misc/2015/12/28/12/5479b2a0e9800000.jpg
 * https://api.gotokeep.com/v1.1/workouts/564025a15372f4dfc94dd36e
 */
public class SingleWorkoutActivity extends BaseActivity {
    static String _id = "564025a15372f4dfc94dd36e";
    private RecyclerView recyclerView;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;
    private String singleWorkoutUrl = "https://api.gotokeep.com/v1.1/workouts/" + _id;
    private String singleWorkoutPioneer = "https://api.gotokeep.com/v1.1/workouts/" + _id + "/pioneer";
    private String singleWorkoutTimeLine = "https://api.gotokeep.com/v1.1/workouts/" + _id + "/timeline";
    private MySingleWorkoutAdapter singleWorkoutAdapter;
    private TextView singleWorkoutName, singleWorkoutDes, singleWorkoutOneDuration, singleWorkoutOneTimes, singleWorkoutOneCalorie;
    private TextView singleWorkoutDiff, singleWorkoutPoint;
    private RatingBar singleWorkoutRating;
    private NetworkImageView singleWorkoutNI;
    private View headView;
    private RecyclerView singleWorkoutItemTwo;
    private ImageView singleWorkoutToDes;
    private SingleWorkoutHorAdapter singleWorkoutHorAdapter;

    public static void goToSingleWorkoutActivity(Context context, String _id) {
        Intent intent = new Intent(context, SingleWorkoutActivity.class);
        SingleWorkoutActivity._id = _id;
        context.startActivity(intent);
    }

    @Override
    protected View getLayoutView() {
        return null;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_single_workout;
    }

    @Override
    protected void initView() {
        headView = LayoutInflater.from(this).inflate(R.layout.item_two_singleworkout, null);
        singleWorkoutNI = (NetworkImageView) findViewById(R.id.singleWorkoutNI);
        singleWorkoutToDes = (ImageView) findViewById(R.id.singleWorkoutToDes);
        singleWorkoutDes = (TextView) findViewById(R.id.singleWorkoutDes);
        singleWorkoutName = (TextView) findViewById(R.id.singleWorkoutName);
        singleWorkoutOneDuration = (TextView) headView.findViewById(R.id.singleWorkoutOneDuration);
        singleWorkoutOneTimes = (TextView) headView.findViewById(R.id.singleWorkoutOneTimes);
        recyclerView = (RecyclerView) findViewById(R.id.singleWorkoutRecycler);
        singleWorkoutOneCalorie = (TextView) headView.findViewById(R.id.singleWorkoutOneCalorie);
        singleWorkoutItemTwo = (RecyclerView) headView.findViewById(R.id.singleWorkoutItemTwo);
        singleWorkoutDiff = (TextView) findViewById(R.id.singleWorkoutDiff);
        singleWorkoutRating = (RatingBar) findViewById(R.id.singleWorkoutRating);
        singleWorkoutPoint = (TextView) findViewById(R.id.singleWorkoutPoint);
    }

    @Override
    protected void initData() {
        //跳转到des

        singleWorkoutRating.setMax(5);
        singleWorkoutHorAdapter = new SingleWorkoutHorAdapter(this);
        singleWorkoutItemTwo.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        singleWorkoutItemTwo.setAdapter(singleWorkoutHorAdapter);
        volleySingleton = VolleySingleton.getInstance();
        imageLoader = volleySingleton._getImageLoader();
        singleWorkoutAdapter = new MySingleWorkoutAdapter(this, headView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(singleWorkoutAdapter);
        StringRequest request = new StringRequest(singleWorkoutUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                final SingleWorkoutData singleWorkoutData = JSON.parseObject(response, SingleWorkoutData.class);
                singleWorkoutNI.setDefaultImageResId(R.mipmap.planmain320_370);
                singleWorkoutNI.setErrorImageResId(R.mipmap.planmain320_370);
                singleWorkoutNI.setImageUrl(singleWorkoutData.getData().getPicture(), imageLoader);
                singleWorkoutName.setText(singleWorkoutData.getData().getName());
                singleWorkoutDes.setText(singleWorkoutData.getData().getDescription());
                singleWorkoutDiff.setText(getDiff(singleWorkoutData.getData().getDifficulty()));
                singleWorkoutPoint.setText(singleWorkoutData.getData().getTrainingpoints().get(0).getName());
                singleWorkoutOneDuration.setText(singleWorkoutData.getData().getDuration() + "分");
                singleWorkoutOneTimes.setText(singleWorkoutData.getData().getSteps().size() + "个");
                singleWorkoutOneCalorie.setText(singleWorkoutData.getData().getCalorie() + "千卡");
                singleWorkoutHorAdapter.addData(singleWorkoutData.getData().getSteps());
                singleWorkoutToDes.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(SingleWorkoutActivity.this, MyWorkoutDesActivity.class);
                        intent.putExtra("content", singleWorkoutData.getData().getDetail());
                        startActivity(intent);
                    }
                });
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(SingleWorkoutActivity.this, "one", Toast.LENGTH_SHORT).show();
            }
        });
        volleySingleton._addRequest(request);
        StringRequest timeRequest = new StringRequest(singleWorkoutTimeLine, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                SingleWorkoutTimeLineData timeLineData = JSON.parseObject(response, SingleWorkoutTimeLineData.class);
                singleWorkoutAdapter.addTimeLineData(timeLineData.getData());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton._addRequest(timeRequest);
//        GsonRequest<SingleWorkoutTimeLineData> timeLineDataGsonRequest = new GsonRequest<SingleWorkoutTimeLineData>(singleWorkoutTimeLine, SingleWorkoutTimeLineData.class, new Response.Listener<SingleWorkoutTimeLineData>() {
//            @Override
//            public void onResponse(SingleWorkoutTimeLineData response) {
//                singleWorkoutAdapter.addTimeLineData(response.getData());
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
////                Toast.makeText(SingleWorkoutActivity.this, singleWorkoutTimeLine, Toast.LENGTH_SHORT).show();
//
//            }
//        }) {
//            @Override
//            public Map<String, String> getHeaders() throws AuthFailureError {
//                Map<String, String> timeLine = new HashMap<>();
//                timeLine.put("Authorization", "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfaWQiOiI1NjY1MDBlMTYzZGIwYzlmNTdiMTQwYWQiLCJ1c2VybmFtZSI6Iuaeq-WQmyIsImF2YXRhciI6Imh0dHA6Ly93eC5xbG9nby5jbi9tbW9wZW4vc2t3MmhSR3lXRWcwUzV0NzhkMlRVUnFhYjBzcWJ4RXB4S1BLUFpEOXpwbTNJbkIySzRjRnhLVEJsemdDb3VIcGlia3RkRUluQ0NFcGpIS2FHZ1lwRkp6SVRIVU5NYTNpYmYvMCIsImlhdCI6MTQ1MzE5OTI4MywiZXhwIjoxNDU1NzkxMjgzLCJpc3MiOiJodHRwOi8vd3d3LmdvdG9rZWVwLmNvbS8ifQ.PlpM4ZTY8s3JwHLoL6WgR2NSkQM6egxkx21rZfWLZjQ");
//                timeLine.put("X-X", "486bba2566a02d3ae31ed4445366a873b89cf910");
//                timeLine.put("Host", "api.gotokeep.com");
//                return timeLine;
//            }
    }

    //难度筛选
    private String getDiff(int difficulty) {
        String difficultyText = "";
        singleWorkoutRating.setNumStars(difficulty);
        switch (difficulty) {
            case 1:
                difficultyText = "K1 零基础";
                break;
            case 2:
                difficultyText = "K1 初学";
                break;
            case 3:
                difficultyText = "K3 进阶";
                break;
            case 4:
                difficultyText = "K4 强化";
                break;

        }
        return difficultyText;
    }
//        volleySingleton._addRequest(timeLineDataGsonRequest);
//    }
}
